The macros described here provide more
sophisticated, high-level looping constructs to complement Emacs
Lisp's basic while loop.
The CL package supports both the simple, old-style meaning of
loopand the extremely powerful and flexible feature known as the Loop Facility or Loop Macro. This more advanced facility is discussed in the following section; see Loop Facility. The simple form ofloopis described here.If
loopis followed by zero or more Lisp expressions, then(loopexprs...)simply creates an infinite loop executing the expressions over and over. The loop is enclosed in an implicitnilblock. Thus,(loop (foo) (if (no-more) (return 72)) (bar))is exactly equivalent to
(block nil (while t (foo) (if (no-more) (return 72)) (bar)))If any of the expressions are plain symbols, the loop is instead interpreted as a Loop Macro specification as described later. (This is not a restriction in practice, since a plain symbol in the above notation would simply access and throw away the value of a variable.)
This macro creates a general iterative loop. Each spec is of the form
(var [init [step]])The loop works as follows: First, each var is bound to the associated init value as if by a
letform. Then, in each iteration of the loop, the end-test is evaluated; if true, the loop is finished. Otherwise, the body forms are evaluated, then each var is set to the associated step expression (as if by apsetqform) and the next iteration begins. Once the end-test becomes true, the result forms are evaluated (with the vars still bound to their values) to produce the result returned bydo.The entire
doloop is enclosed in an implicitnilblock, so that you can use(return)to break out of the loop at any time.If there are no result forms, the loop returns
nil. If a given var has no step form, it is bound to its init value but not otherwise modified during thedoloop (unless the code explicitly modifies it); this case is just a shorthand for putting a(let ((var init)) ...)around the loop. If init is also omitted it defaults tonil, and in this case a plain ‘var’ can be used in place of ‘(var)’, again following the analogy withlet.This example (from Steele) illustrates a loop which applies the function
fto successive pairs of values from the listsfooandbar; it is equivalent to the call(mapcar* 'f foo bar). Note that this loop has no body forms at all, performing all its work as side effects of the rest of the loop.(do ((x foo (cdr x)) (y bar (cdr y)) (z nil (cons (f (car x) (car y)) z))) ((or (null x) (null y)) (nreverse z)))
This is to
dowhatlet*is tolet. In particular, the initial values are bound as if bylet*rather thanlet, and the steps are assigned as if bysetqrather thanpsetq.Here is another way to write the above loop:
(do* ((xp foo (cdr xp)) (yp bar (cdr yp)) (x (car xp) (car xp)) (y (car yp) (car yp)) z) ((or (null xp) (null yp)) (nreverse z)) (push (f x y) z))
This is a more specialized loop which iterates across the elements of a list. list should evaluate to a list; the body forms are executed with var bound to each element of the list in turn. Finally, the result form (or
nil) is evaluated with var bound tonilto produce the result returned by the loop. Unlike with Emacs's built indolist, the loop is surrounded by an implicitnilblock.
This is a more specialized loop which iterates a specified number of times. The body is executed with var bound to the integers from zero (inclusive) to count (exclusive), in turn. Then the
resultform is evaluated with var bound to the total number of iterations that were done (i.e.,(max 0count)) to get the return value for the loop form. Unlike with Emacs's built indolist, the loop is surrounded by an implicitnilblock.
This loop iterates over all interned symbols. If obarray is specified and is not
nil, it loops over all symbols in that obarray. For each symbol, the body forms are evaluated with var bound to that symbol. The symbols are visited in an unspecified order. Afterward the result form, if any, is evaluated (with var bound tonil) to get the return value. The loop is surrounded by an implicitnilblock.
This is identical to
do-symbolsexcept that the obarray argument is omitted; it always iterates over the default obarray.
See Mapping over Sequences, for some more functions for iterating over vectors or lists.